package yannan.voiplock4ble.utils;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.WindowManager;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import yannan.voiplock4ble.base.BaseFragment;

/**
 * 说明：一般工具类
 * 作者：mainTel
 * 时间：2016/8/22 11:31
 * 备注：
 */
public class CommonUtils {

    /**
     * 获取SDK版本号
     *
     * @return
     */
    public static int getAndroidOSVersion() {
        int osVersion;
        try {
            osVersion = Integer.valueOf(android.os.Build.VERSION.SDK);
        } catch (NumberFormatException e) {
            osVersion = 0;
        }

        return osVersion;
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public static void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }


    /**
     * 校验手机号是否正确
     *
     * @param phone
     * @return
     */
    public static boolean validPhone(String phone) {
        if (TextUtils.isEmpty(phone.replace(" ", ""))) {
            return false;
        } else if (phone.length() < 11) {
            return false;
        } else {
            String phoneRule = "^1[3|4|5|7|8][0-9]\\d{8}$";
            Pattern p = Pattern.compile(phoneRule);
            Matcher match = p.matcher(phone);
            if (match.matches()) {
                return true;
            }
        }
        return false;
    }


    /**
     * 校验是否包含特殊字符
     *
     * @param str
     * @return 包含返回true  不包含返回false
     */
    public static boolean checkSpecialChar(String str) {
        String regEx = "[`~!@#$%^&*()-+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.find();
    }


    /**
     * 通用的序列化json成list的方法
     *
     * @param clazz
     * @param json
     * @param <T>
     * @return
     */
    public static <T> List<T> analyseJson(Class<T> clazz, String json) {
        Type type = new TypeToken<ArrayList<JsonObject>>() {
        }.getType();
        ArrayList<JsonObject> jsonObjs = new Gson().fromJson(json, type);

        ArrayList<T> listOfT = new ArrayList<>();
        if (jsonObjs != null)
            for (JsonObject jsonObj : jsonObjs) {
                listOfT.add(new Gson().fromJson(jsonObj, clazz));
            }

        return listOfT;
    }


    /**
     * 切换Fragment
     *
     * @param show
     * @param isFirst
     */
    public static BaseFragment showHideFragment(AppCompatActivity context, BaseFragment show, BaseFragment hide, int id, boolean isFirst) {
        FragmentManager fm = context.getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (isFirst) {
            ft.add(id, show);
        } else {
            ft.show(show);
        }
        if (hide != null) {
            ft.hide(hide);
        }
        hide = show;
        ft.commit();
        return hide;
    }
}
